package com.home.cop.employee.controller;

import com.home.cop.base.BaseController;
import com.home.cop.employee.model.Department;
import com.home.cop.employee.service.DepartmentService;
import com.home.cop.sysinfo.model.UserSession;
import com.home.cop.system.BusinessException;
import com.home.cop.system.RequiredAuth;
import com.home.cop.system.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/dept")
@Api("部门信息相关")
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService deptService;

    @RequiredAuth
    @PostMapping("/list")
    @ApiOperation(notes = "查询部门列表",value = "根据条件查询部门列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "instName", value = "机构名字", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deptName", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "director", value = "部门负责人", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "角色编号", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "jobId", value = "职务编号", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：0-启用，1-禁用", required = false, dataType = "Integer", paramType = "query")
    })
    public Result list(@RequestParam(value = "instName",required = false)String instName,
                       @RequestParam(value = "director",required = false)String director,
                       @RequestParam(value = "deptName",required = false)String deptName){
        Map<String,Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(instName)){
            params.put("instName",instName.trim());
        }
        if (StringUtils.isNotBlank(director)){
            params.put("director",director.trim());
        }
        if (StringUtils.isNotBlank(deptName)){
            params.put("deptName",deptName);
        }

        UserSession userSession = getUserSession();
        if (userSession.getInstId() != 0 ){
            params.put("instId",userSession.getInstId());
        }
        return Result.returnSuccess(deptService.list(params,getPageNum(),getPageSize()));
    }


    @RequiredAuth
    @PostMapping("/updateInfo")
    @ApiOperation(notes = "修改信息",value = "修改信息")
    public Result updateInfo(@RequestBody Department department){
        UserSession userSession = getUserSession();
        if (userSession.getInstId() != 0 ){
            return Result.returnFail().setMessage("无权限！");
        }
        deptService.updateInfo(department);
        return Result.returnSuccess().setMessage("修改成功!");
    }

    @RequiredAuth
    @PostMapping("/add")
    @ApiOperation(notes = "新增部门",value = "新增部门")
    public Result add(@RequestBody Department department){
        UserSession userSession = getUserSession();
        if (userSession.getInstId() != 0 ){
            return Result.returnFail().setMessage("无权限！");
        }
        deptService.insert(department);
        return Result.returnSuccess().setMessage("新增成功!");
    }

    @RequiredAuth
    @PostMapping("/delete")
    @ApiOperation(notes = "删除部门",value = "删除部门")
    @ApiImplicitParams({@ApiImplicitParam(name = "deptId", value = "部门编号", required = false, dataType = "String", paramType = "query"),
    })
    public Result delete(@RequestParam("deptId")Integer deptId){
        RoleCheck();
        deptService.delete(deptId);
        return Result.returnSuccess().setMessage("删除成功!");
    }
}
